//
//  ViewController.m
//  ResponserChain
//
//  Created by 苏沫离 on 2018/11/21.
//  Copyright © 2018 苏沫离. All rights reserved.
//

#define SCREEN_WIDTH CGRectGetWidth(UIScreen.mainScreen.bounds)
#define SCREEN_HEIGHT CGRectGetHeight(UIScreen.mainScreen.bounds)
#define NSLog(FORMAT, ...)  fprintf(stderr,"%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

#import "ViewController.h"
#import "AppDelegate.h"
#import "UIView+Responser.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = UIColor.whiteColor;
    self.view.tag = 20;
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(30, 100, SCREEN_WIDTH - 60, 200)];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.tag = 21;
    button.titleEdgeInsets = UIEdgeInsetsMake(16, 0, 0, 0);
    button.backgroundColor = [UIColor colorWithRed:255.0/255.0 green:69/255.0 blue:201/255.0 alpha:1.0];
    [button setTitle:@"button 21" forState:UIControlStateNormal];
    button.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
    [self.view addSubview:button];
    
    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(30, CGRectGetMaxY(button.frame) + 100, SCREEN_WIDTH - 60, 100)];
    button1.tag = 22;
    button1.titleLabel.font = [UIFont systemFontOfSize:14];
    button1.backgroundColor = [UIColor colorWithRed:255/255.0 green:65/255.0 blue:67/255.0 alpha:1.0];
    [button1 setTitle:@"button 22" forState:UIControlStateNormal];
    [self.view addSubview:button1];

    
    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(100, CGRectGetMaxY(button1.frame) - 30, SCREEN_WIDTH - 200, 200)];
    lable.tag = 23;
    lable.font = [UIFont systemFontOfSize:14];
    lable.backgroundColor = [[UIColor colorWithRed:255/255.0 green:249/255.0 blue:24/255.0 alpha:1.0] colorWithAlphaComponent:0.8];
    lable.text = @"lable 23";
    lable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:lable];
    
    NSLog(@"viewController ==== %@",self);
    NSLog(@"viewController ==== %@",lable.viewController);

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    [self setWindowView];
    
    
    
//    NSLog(@"self.view =============== %@",self.view);
//    NSLog(@"self.view.subviews ====== %@",self.view.subviews);
//    NSLog(@"window ================== %@",UIApplication.sharedApplication.delegate.window);
//    NSLog(@"window.subviews ========= %@",UIApplication.sharedApplication.delegate.window.subviews);
}

- (void)setWindowView
{
    UIApplication.sharedApplication.delegate.window.tag = 10;

    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(30, 40, 140, 40)];
    lable.backgroundColor = [UIColor redColor];
    lable.tag = 11;
    lable.font = [UIFont systemFontOfSize:14];
    lable.textColor = UIColor.whiteColor;
    lable.text = @"window label 11";
    lable.textAlignment = NSTextAlignmentCenter;
    [UIApplication.sharedApplication.delegate.window addSubview:lable];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(200, 40, 140, 40)];
    button.backgroundColor = [UIColor blueColor];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.tag = 12;
    [button setTitle:@"window button 12" forState:UIControlStateNormal];
    [UIApplication.sharedApplication.delegate.window addSubview:button];
}

- (void)getNextResponder
{
    [(AppDelegate *)UIApplication.sharedApplication.delegate logNextResponder];
    [UIApplication.sharedApplication logNextResponder];
    [UIApplication.sharedApplication.delegate.window logNextResponder];
    [UIApplication.sharedApplication.delegate.window.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj logNextResponder];
    }];
    [self logNextResponder];
    [self.view.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj logNextResponder];
    }];
}



@end
